package org.jeecg.modules.erp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.erp.base.entity.ErpGoodsIngredientsReplace;
import org.jeecg.modules.erp.base.entity.ErpGoodsIngredientsReplaceInfo;
import org.jeecg.modules.erp.base.mapper.ErpGoodsIngredientsReplaceInfoMapper;
import org.jeecg.modules.erp.base.mapper.ErpGoodsIngredientsReplaceMapper;
import org.jeecg.modules.erp.base.service.IErpGoodsIngredientsReplaceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 原料替代品管理明细表
 * @Author: jeecg-boot
 * @Date:   2024-05-24
 * @Version: V1.0
 */
@Service
public class ErpGoodsIngredientsReplaceInfoServiceImpl extends ServiceImpl<ErpGoodsIngredientsReplaceInfoMapper, ErpGoodsIngredientsReplaceInfo> implements IErpGoodsIngredientsReplaceInfoService {
	
	@Autowired
	private ErpGoodsIngredientsReplaceInfoMapper erpGoodsIngredientsReplaceInfoMapper;

	@Autowired
	private ErpGoodsIngredientsReplaceMapper erpGoodsIngredientsReplaceMapper;
	
	@Override
	public List<ErpGoodsIngredientsReplaceInfo> selectByMainId(String mainId) {
		return erpGoodsIngredientsReplaceInfoMapper.selectByMainId(mainId);
	}

	@Override
	public void saveMain(ErpGoodsIngredientsReplaceInfo info) {
		ErpGoodsIngredientsReplace replace = erpGoodsIngredientsReplaceMapper.selectById(info.getIngredientsId());
		if(replace.getMaterialCode().equals(info.getReplaceMaterialCode())){
			throw new RuntimeException("与原物料一致，无法添加");
		}
		ErpGoodsIngredientsReplaceInfo erpGoodsIngredientsReplaceInfo = erpGoodsIngredientsReplaceInfoMapper.selectOne(new LambdaQueryWrapper<ErpGoodsIngredientsReplaceInfo>()
					.eq(ErpGoodsIngredientsReplaceInfo::getReplaceMaterialCode, info.getReplaceMaterialCode())
					.eq(ErpGoodsIngredientsReplaceInfo::getIngredientsId, info.getIngredientsId()));
			if(erpGoodsIngredientsReplaceInfo != null){
				info.setId(erpGoodsIngredientsReplaceInfo.getId());
			}
			this.saveOrUpdate(info);
	}
}
